package com.lianzhihui.romeex.ui.base;

import android.app.Activity;
import android.app.Dialog;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.lianzhihui.romeex.R;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import butterknife.ButterKnife;

import static com.qmuiteam.qmui.widget.dialog.QMUITipDialog.Builder.ICON_TYPE_LOADING;

/**
 * createBy: echo
 * date: 2018/8/26
 * description:
 */
public abstract class BaseDialog extends Dialog {
    protected Context context;
    protected View dialogView;
    protected WindowManager.LayoutParams params;
    private QMUITipDialog qmuiTipDialogLoading;

    public BaseDialog(@NonNull Context context) {
        this(context, R.style.CustomDialog);
    }

    public BaseDialog(Context context, int style) {
        super(context, R.style.QMUI_Dialog);
        this.context = context;
        init(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void init(Context context) {
        dialogView = LayoutInflater.from(context).inflate(provideViewId(), null);
        ButterKnife.bind(this, dialogView);
        setContentView(dialogView);
        setMaxWidthBottom();
        setB2T();
    }


    protected abstract int provideViewId();

    protected abstract void initView();

    protected void showLoading(String title) {
        if (qmuiTipDialogLoading == null) {
            qmuiTipDialogLoading = new QMUITipDialog.Builder(getContext()).setIconType(ICON_TYPE_LOADING).setTipWord(title).create();
        }
        qmuiTipDialogLoading.show();
    }

    protected void dismissLoading() {
        if (qmuiTipDialogLoading != null) {
            qmuiTipDialogLoading.dismiss();
        }
    }

    public void copyString(String string) {
        ClipboardManager cm = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
        cm.setText(string);
        Toast.makeText(getContext(), getContext().getResources().getString(R.string.toast_copy_success), Toast.LENGTH_SHORT).show();
    }

    /**
     * EditText获取焦点并显示软键盘
     */
    public static void showSoftInputFromWindow(Activity activity, EditText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }

    /**
     * 宽充满屏幕
     *
     * @return
     */
    public BaseDialog fullWidth() {
        if (params == null) {
            params = getWindow().getAttributes();
        }
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(params);
        return this;
    }

    /**
     * 位置在屏幕最底部
     *
     * @return
     */
    public BaseDialog bottom() {
        if (params == null) {
            params = getWindow().getAttributes();
        }
        params.gravity = Gravity.BOTTOM;
        getWindow().setAttributes(params);
        return this;
    }

    /**
     * 位置在屏幕中间
     *
     * @return
     */
    public BaseDialog center() {
        if (params == null) {
            params = getWindow().getAttributes();
        }
        params.gravity =Gravity.CENTER ;
        getWindow().setAttributes(params);

        Window window = getWindow();
        window.setWindowAnimations(R.style.dialog_animation_tr);
        setWidthWrap();
        return this;
    }

    /**
     * 位置在右上角
     *
     * @return
     */
    public BaseDialog rightTop() {
        if (params == null) {
            params = getWindow().getAttributes();
        }
        params.gravity = Gravity.RIGHT | Gravity.TOP;
        getWindow().setAttributes(params);
        return this;
    }

    /**
     * 位置在右上角
     *
     * @return
     */
    public BaseDialog leftTop() {
        if (params == null) {
            params = getWindow().getAttributes();
        }
        params.gravity = Gravity.LEFT | Gravity.TOP;
        getWindow().setAttributes(params);
        return this;
    }

    @Override
    public void show() {
        super.show();
    }

    /**
     * 上到下
     */
    public void setT2B() {
        Window window = getWindow();
        window.setWindowAnimations(R.style.dialog_animation);
    }

    /**
     * 下到上
     */
    public void setB2T() {
        Window window = getWindow();
        window.setWindowAnimations(R.style.dialogWindowAnimButtomToTop);
    }

    /**
     * 设置全屏
     */
    public void setWindowFull() {
        Window window = getWindow();

        Resources resources = getContext().getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        float density = dm.density;
        int width = dm.widthPixels;
        int screenHeight = dm.heightPixels;

        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        int statusBarHeight = resources.getDimensionPixelSize(resourceId);

        int dialogHeight = screenHeight - statusBarHeight;
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, dialogHeight == 0 ? ViewGroup.LayoutParams.MATCH_PARENT : dialogHeight);
        window.setBackgroundDrawableResource(R.color.transparent);
    }

    /**
     * 水平max 底部显示
     */
    public void setMaxWidthBottom() {
        Window window = getWindow();
        if (params == null) {
            params = getWindow().getAttributes();
        }
        params.gravity = Gravity.BOTTOM;
        window.setAttributes(params);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setBackgroundDrawableResource(R.color.transparent);
    }

    /**
     * 设置自适应宽度
     */
    public void setWidthWrap() {
        Window window = getWindow();
        if (params == null) {
            params = getWindow().getAttributes();
        }
        window.setAttributes(params);
        window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setBackgroundDrawableResource(R.color.transparent);
    }

    /**
     * 水平max
     */
    public void setMaxWith() {
        Window window = getWindow();
//        if (params == null) {
//            params = getWindow().getAttributes();
//        }
//        params.gravity = Gravity.BOTTOM;
//        window.setAttributes(params);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setBackgroundDrawableResource(R.color.transparent);
    }

}
